package com.oa.ylms.apps.actions.regulations;

import java.util.List;

import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.regulations.IRegulationCategoryService;
import com.oa.ylms.apps.business.regulations.IRegulationInfoService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.regulations.RegulationCategoryValue;
import com.oa.ylms.apps.entity.regulations.RegulationInfoValue;

@SuppressWarnings("serial")
public class RegulationInfoAction extends BasicAction {
	
	private List<RegulationInfoValue> infoList;
	private List<RegulationCategoryValue> categoryList;
	private RegulationInfoValue regulationInfo;
	private IRegulationInfoService infoService;
	private IRegulationCategoryService categoryService;
	
	private Integer regulationId;
	private String regulationIds;
	private Integer categoryId;
	private String publishTime;
	private String staffName;
	private String title;
	private String content;
	
	private String str;
 
	// Action method

	public String execute() throws Exception {
		regulationInfo = infoService.queryRegulationById(regulationId);
		return "show";
	}
	
	public String show() throws Exception {
		regulationInfo = infoService.queryRegulationById(regulationId);
		return "showRegulation";
	}
	
	
	public String toAdd() throws Exception {
		categoryList = categoryService.queryCategoryByStates(IDEDStates.ENABLE);
		return "toAdd";
	}
	
	public String add() throws Exception{
		staffInfo = (StaffInfoValue)session.get("login_staffInfo");
		content = content.replace("<", "&lt;").replace(">", "&gt;").replace("\"", "'");
		if(infoService.addRegulationInfo(categoryId, title, content, staffInfo) >0){
			str = "SUCCESS!";
			return "add";
		}
		return "false";
	}
	
	public String toEdit() throws Exception {
		categoryList = categoryService.queryCategoryByStates(IDEDStates.ENABLE);
		regulationInfo = infoService.queryRegulationById(regulationId);
		title = regulationInfo.getTitle();
		content = regulationInfo.getContent();
		categoryId = regulationInfo.getRegulationCategory().getCategoryId();
		return "toEdit";
	}
	
	public String modify() throws Exception{
		staffInfo = (StaffInfoValue)session.get("login_staffInfo");
		content = content.replace("<", "&lt;").replace(">", "&gt;").replace("\"", "'");
		if(infoService.modifyRegulationInfo(regulationId, categoryId, title, content, staffInfo))
			return "modify";
		return "false";
	}
	
	public String toManager() throws Exception{
		if("".equals(publishTime))
			publishTime = null;
		if("".equals(title))
			title = null;
		if("".equals(staffName))
			staffName = null;
		
//		if(publishTime != null)
//			publishTime = new String(publishTime.getBytes("ISO-8859-1"),"UTF-8");
//		if(title != null)
//			title = new String(title.getBytes("ISO-8859-1"),"UTF-8");
//		if(staffName != null)
//			staffName = new String(staffName.getBytes("ISO-8859-1"),"UTF-8");
		
		totalpage = infoService.countTPages(categoryId, publishTime, title, staffName, rows);
		count = infoService.getTotalNumber();
		if(count%rows==0&&totalpage+1==currpage){
				currpage=totalpage;
		}
		categoryList = categoryService.queryCategoryByStates(IDEDStates.ENABLE);
		infoList = infoService.queryRegulationsWithPage(categoryId, publishTime, title, staffName, rows, currpage);
		return "toManager";
	}
	
	public String delete() throws Exception{
		if(infoService.deleteRegulationInfo(regulationIds)){
			return "delete";
		}
		return "false";

	}
	
	public String toShow() throws Exception{
		if("".equals(publishTime))
			publishTime = null;
		if("".equals(title))
			title = null;
		if("".equals(staffName))
			staffName = null;
		
//		if(publishTime != null)
//			publishTime = new String(publishTime.getBytes("ISO-8859-1"),"UTF-8");
//		if(title != null)
//			title = new String(title.getBytes("ISO-8859-1"),"UTF-8");
//		if(staffName != null)
//			staffName = new String(staffName.getBytes("ISO-8859-1"),"UTF-8");
		
		totalpage = infoService.countTPages(categoryId, publishTime, title, staffName, rows);
		count = infoService.getTotalNumber();
		categoryList = categoryService.queryCategoryByStates(IDEDStates.ENABLE);
		infoList = infoService.queryRegulationsWithPage(categoryId, publishTime, title, staffName, rows, currpage);
		return "toShow";
	}
	
	public String toShowRegulation() throws Exception{
		if("".equals(publishTime))
			publishTime = null;
		if("".equals(title))
			title = null;
		if("".equals(staffName))
			staffName = null;
		
//		if(publishTime != null)
//			publishTime = new String(publishTime.getBytes("ISO-8859-1"),"UTF-8");
//		if(title != null)
//			title = new String(title.getBytes("ISO-8859-1"),"UTF-8");
//		if(staffName != null)
//			staffName = new String(staffName.getBytes("ISO-8859-1"),"UTF-8");
		
		totalpage = infoService.countTPages(categoryId, publishTime, title, staffName, rows);
		count = infoService.getTotalNumber();
		categoryList = categoryService.queryCategoryByStates(IDEDStates.ENABLE);
		infoList = infoService.queryRegulationsWithPage(categoryId, publishTime, title, staffName, rows, currpage);
		return "toShowRegulation";
	}
	
	// Action property accessors

	public List<RegulationInfoValue> getInfoList() {
		return infoList;
	}

	public void setInfoList(List<RegulationInfoValue> infoList) {
		this.infoList = infoList;
	}

	public List<RegulationCategoryValue> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<RegulationCategoryValue> categoryList) {
		this.categoryList = categoryList;
	}

	public RegulationInfoValue getRegulationInfo() {
		return regulationInfo;
	}

	public void setRegulationInfo(RegulationInfoValue regulationInfo) {
		this.regulationInfo = regulationInfo;
	}

	public IRegulationInfoService getInfoService() {
		return infoService;
	}

	public void setInfoService(IRegulationInfoService infoService) {
		this.infoService = infoService;
	}

	public IRegulationCategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(IRegulationCategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public Integer getRegulationId() {
		return regulationId;
	}

	public void setRegulationId(Integer regulationId) {
		this.regulationId = regulationId;
	}

	public String getRegulationIds() {
		return regulationIds;
	}

	public void setRegulationIds(String regulationIds) {
		this.regulationIds = regulationIds;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public String getPublishTime() {
		return publishTime;
	}

	public void setPublishTime(String publishTime) {
		this.publishTime = publishTime;
	}

	public String getStaffName() {
		return staffName;
	}

	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public String getStr() {
		return str;
	}
	
}
